package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.AdminUserModel;
import com.btkjgc.service.intf.AdminUserManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class AdminUserManageController extends BaseController{

	static Logger logger = Logger.getLogger(AdminUserManageController.class);

	@Autowired
	private AdminUserManageService_Intf adminuserManageService;

	@RequestMapping(value = "admin_user_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,AdminUserModel adminUserModel) {

		ModelAndView mv = new ModelAndView("admin_user_manage");
		getAdminUserList(request, mv,adminUserModel);
		return mv;
	}

	@RequestMapping(value = "edit_admin_user", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public AjaxObj editAdminUser(AdminUserModel adminUserModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		System.out.println("EditAdminUser");
		AjaxObj ajaxObj=adminuserManageService.updateAdminUser(adminUserModel);

		return ajaxObj;
	}

	@RequestMapping(value = "batchdel_admin_user", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchdelAdminUser(HttpServletRequest request, String idStr) {
		String[] ids = idStr.split(",");
		for (int i = 0; i < ids.length; i++) {
			int adminUserId = Integer.valueOf(ids[i]);
			adminuserManageService.deleteById(adminUserId);
		}
		return "redirect:/admin_user_manage";
	}

	@SuppressWarnings("unchecked")
	private void getAdminUserList(HttpServletRequest request, ModelAndView mv, AdminUserModel adminUserModel) {
 
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "operdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr = { "loginname", "like", adminUserModel.getLoginname()};
		paramList.add(arr);
		
		String sql = "select * from (SELECT\n"
				+ "	aut.*,\n"
				+ "  CASE aut.companytype\n"
				+ "            WHEN 2 THEN (select aut2.name from account_user_t aut2 where aut2.id = aut.companyid  and aut2.del = 0) \n"
				+ "            WHEN 1 THEN (select pct.name from property_company_t pct where aut.companyid = pct.id and pct.del = 0) \n"
				+ "         END as companyname,\n"
				+ " (select ct.name from cities_t ct where aut.cityid = ct.id ) as cityname,\n"
				+ " (select rt.name from role_t rt where id in(select roleid from admin_user_role_t aurt where aurt.userid=aut.id)  limit 1 ) as rolename,\n"
				+ " (select rt.level from role_t rt where id in(select roleid from admin_user_role_t aurt where aurt.userid=aut.id)  limit 1 ) as roleLevel\n"
				+ "FROM\n" + "	admin_user_t aut\n" + " \n" 
				+ "WHERE\n"
				+ "	aut.del = 0 ) a  where rolelevel<="+getLoginUser().getRoleLevel();

		sql = adminuserManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = adminuserManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<AdminUserModel> adminUserList = adminuserManageService.getList(
				AdminUserModel.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		adminuserManageService.setListData(request, mv, pageIndex, totalRecord,
				adminUserList);

	}

	@RequestMapping(value = "get_adminuser_by_id")
	@ResponseBody
	public AdminUserModel getAdminuserById(Integer id) {

		String sql = "SELECT\n"
				+ "	aut.*,aurt.roleId\n"
				+ "FROM\n"
				+ "	admin_user_t aut\n"
				+ "LEFT JOIN admin_user_role_t aurt  on aut.id = aurt.userid  \n"
				+ "WHERE 	aut.id = ?";

		AdminUserModel adminUserModel = (AdminUserModel) HibernateUtil
				.executeSQLQueryObject(sql, AdminUserModel.class, id);

		return adminUserModel;
	}

	@RequestMapping(value = "add_admin_user/{a}")
	@ResponseBody
	public  Integer jsonAddAdminUser(HttpServletRequest request,
			@PathVariable("a") int a, AdminUserModel adminUserModel) {
 
		if (adminUserModel.getLoginname().length() > 0) {
			adminUserModel.setCompanyid(Integer.valueOf(adminUserModel
					.getCompanyStr().split("-")[1]));
			adminUserModel.setCompanytype(Integer.valueOf(adminUserModel
					.getCompanyStr().split("-")[0]));
			adminUserModel.setDel(0);
			adminUserModel.setCreatedate(new Date());
			Integer reInt = adminuserManageService
					.insertAdminUser(adminUserModel);
			return reInt;

		}
		return -1;
	}

}
